/* 
	AJAX - Asynchronous JavaScript and XML (--DEMO--)
	Author: Rams
*/

//create a object xmlHttpRequestHandler
function xmlHttpRequestHandler(properties) {
	
	//xmlHttpRequestHandler Properties variable
	if (typeof properties == 'undefined')
		properties = null;

	this.props = properties;

	//first Initiates xmlHttpRequestHandler
	this.init();
}

//function: to initialize the handler
xmlHttpRequestHandler.prototype.init = function() {
	
	this.responseText = null;
	this.responseXML  = null;
	this.responseBody = null;
	this.statusText	  = null;

	this.XmlHttpRequestObject = null;
	this.browserType = "unknown";
	this.callback = null;
	this.data = null;
	
	//string method, string url, boolean asynch, string username, string password
	this.method = "GET";
	this.URL = "";
	this.asynch = true;
	this.username = null;
	this.password = null;
	this.opened = false;
	this.processHandler = null;
	this.callback_data = new Array();

	//MISC
	this.inputHandler = null;

	if (this.props != null )
	{
		this.override();
	}
}

//function: to set the values of (method, url, asynch, username, password)
xmlHttpRequestHandler.prototype.override = function() {
	if (this.props == null) return;

	if (typeof this.props['METHOD'] != 'undefined')
		this.method = this.props['METHOD'];

	if (typeof this.props['URL'] != 'undefined')
		this.URL = this.props['URL'];

	if (typeof this.props['ASYNCH'] != 'undefined')
		this.asynch = this.props['ASYNCH'];

	if (typeof this.props['USERNAME'] != 'undefined')
		this.username = this.props['USERNAME'];

	if (typeof this.props['PASSWORD'] != 'undefined')
		this.password = this.props['PASSWORD']; }

//function: to set the FORM Method
xmlHttpRequestHandler.prototype.setMethod = function(method) {
	this.method = method;
}

//function: to set the URL
xmlHttpRequestHandler.prototype.setURL = function(url) {
	this.URL = url;
}

//function: to set the Asynchronization or Synchronization 
xmlHttpRequestHandler.prototype.setType = function(type) {
	this.asynch = type;
}

//function: to set the User
xmlHttpRequestHandler.prototype.setUser = function(user, pass) {
	this.username = user;
	this.password = pass;
}

//function: to set POST data
xmlHttpRequestHandler.prototype.setData = function(dt) {
	this.data = dt;
}

//function: to set callback data
xmlHttpRequestHandler.prototype.setCallBackData = function(key, value) {
	this.callback_data[key] = value;
}

//create function : creates XML HttpRequest Object 
xmlHttpRequestHandler.prototype.create = function() {

	if (this.XmlHttpRequestObject != null) return;
	
	if (typeof XMLHttpRequest != "undefined") {
		// look for Mozilla compatible browsers
		this.XmlHttpRequestObject = new XMLHttpRequest();
		this.browserType = "Mozilla/IE7";
	} 
	else if (window.ActiveXObject) {
		// look up the highest possible MSXML version
		var tryPossibleVersions=["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];

		for (i = 0; i < tryPossibleVersions.length; i++) {
			try{

				this.XmlHttpRequestObject = new ActiveXObject(tryPossibleVersions[i]);
				this.browserType = "IE/"+tryPossibleVersions[i];
				break;
			}
			catch (xmlHttpRequestObjectError){
				//ignore
			}
		}
	}
}

//function: to open a HTTP request
xmlHttpRequestHandler.prototype.open = function() {

	//first creates a HTTP request object
	this.create();
	if (typeof(netscape) != 'undefined')
	{
		//netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	}
	//Opens a HTTP request
	try	{
		if (this.username == null) {
			this.XmlHttpRequestObject.open(this.method, this.URL, this.asynch);
		}
		else {
			this.XmlHttpRequestObject.open(this.method, this.URL, this.asynch, this.username, this.password);
		}
		this.opened = true;
	}
	catch (e) {
		alert("HTTP Request doesn't open:\n\n " + e);
	}
}

//function: Sends a HTTP request to the server and receives a response
xmlHttpRequestHandler.prototype.send = function(callbackFunc) {

	//Then Opens a HTTP request object
	this.open();
	//this.processHandler = new processRequestHandler(this);
	var self = this;
	var cbh = new Object();

	//Processing a Request: BEGIN
	cbh.processRequest = function() {

		if(self.XmlHttpRequestObject.readyState == 4) { // All the data has been received. 

			if(self.XmlHttpRequestObject.status == 200) { // status is 'OK'

				//process response here
				self.responseText = self.XmlHttpRequestObject.responseText;
				self.responseXML = self.XmlHttpRequestObject.responseXML;
				self.statusText = self.XmlHttpRequestObject.statusText;

				if (typeof self.XmlHttpRequestObject.responseBody != 'undefined')
					self.responseBody = self.XmlHttpRequestObject.responseBody;

				if (self.callback == null) return;
				self.callback(self);
			}
		}
	}
	//Processing a Request: END

	if (typeof callbackFunc != 'undefined')
	{
		this.callback = callbackFunc;
	}

	//If request made in POST method
	if (this.method == "POST")
	{
		this.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
	}

	//Sends a HTTP request to the server and receives a response
	try	{
		this.XmlHttpRequestObject.onreadystatechange = cbh.processRequest; //this.processHandler.processRequest;
		this.XmlHttpRequestObject.send(this.data);
	}
	catch (e) {
		//alert("HTTP Request doesn't send  to server:\n\n " + e.description);
	}
}

//Sets HTTP headers to the request
xmlHttpRequestHandler.prototype.setRequestHeader = function(headerName, headerValue) {
	try	{
		this.XmlHttpRequestObject.setRequestHeader(headerName, headerValue);
	}
	catch (e) {
		//alert("Set Header failed:\n\n " + e.description);
	}
}

//Returns all the response headers. Returns null if readyState is other than 3 or 4.
xmlHttpRequestHandler.prototype.getAllResponseHeaders = function() {
	try	{
		return this.XmlHttpRequestObject.getAllResponseHeaders();
	}
	catch (e) {
		return null;
	}
}

//Returns a specified response header. Returns null if readyState is other than 3 or 4. 
xmlHttpRequestHandler.prototype.getResponseHeader = function(headerName) {
	try	{
		return this.XmlHttpRequestObject.getResponseHeader(headerName);
	}
	catch (e) {
		return null;
	}
}

function InputHandler(inputObj) {
	this.input = inputObj;
	this.name = inputObj.name;
	this.value = inputObj.vaue;
	this.className = inputObj.className;
}


//Implementation
/*
var xmlhttp_config =
{"URL":"http://192.168.6.66/~ramnarayana/ajax.php","METHOD":"POST","ASYNCH":true}
var xmlHRH = new xmlHttpRequestHandler(xmlhttp_config);

function doCommand(ob) {
	//alert(ob.responseText);
	if(ob.responseText != '') {
		try{
			eval(ob.responseText);
		} catch(e) {
		}
	}
	//alert(ob.getAllResponseHeaders());
}

function alwaysDOIT() {
	//xmlHRH.setData("name=scott&email=stiger@foocorp.com");
	xmlHRH.send(doCommand);
	setTimeout("alwaysDOIT()", 8000);
}

//Recursive
alwaysDOIT();
*/